package com.alibaba.ai.service;

import com.alibaba.ai.entity.*;
import com.alibaba.ai.util.zHousingDTO;
import com.alibaba.ai.util.zHousingIdDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2020/5/28.
 */
@Service
public class ZHousingService {

    @Resource
    private com.alibaba.ai.dao.zHousingMapper zHousingMapper;

    //公用方法（相同代码的简单封装）
    public void zHousingFF(List<zHousing> zHousings, List<zHousingDTO> zHousingDtos) {

        for (com.alibaba.ai.entity.zHousing zHousing : zHousings) {
            zHousingDTO Dto = new zHousingDTO();//创建对象
            zImage ZImages = zHousingMapper.zImage(zHousing.getzImageId());//查图片
            zMoney ZMoneys = zHousingMapper.zMoney(zHousing.getzMoneyId());//查租金表
            BeanUtils.copyProperties(zHousing, Dto);//将 gj 内容拷贝的到 zHousingDtos（ Dto ）中；
            Dto.setZImage(ZImages);
            Dto.setZmoney(ZMoneys);
            zHousingDtos.add(Dto);
        }

    }


    //个人房源 （房源类型为  住宅  ）
    public List<zHousingDTO> getzHousingDTOGR() {
        List<zHousingDTO> zHousingDtos = new ArrayList<>();

        List<zHousing> zHousings = zHousingMapper.getzHousingGR();//根据类型为住宅查询
        zHousingFF(zHousings, zHousingDtos);
        return zHousingDtos;
    }


    //精选房源  （装修程度为   精装修和豪华装修  ）
    public List<zHousingDTO> getzHousingDTOJX() {
        List<zHousingDTO> zHousingDtos = new ArrayList<>();
        List<zHousing> zHousings = zHousingMapper.getzHousingJX();//房源装修程度为精装修和豪华装修
        zHousingFF(zHousings, zHousingDtos);
        return zHousingDtos;
    }


    //房源信息（循环展示全部简单信息）
    public List<zHousingDTO> getzHousingDTOBy() {
        List<zHousingDTO> zHousingDtos = new ArrayList<>();//房源信息（展示集合 循环展示）
        List<zHousing> zHousings = zHousingMapper.zHousingList();//房源集合
        zHousingFF(zHousings, zHousingDtos);
        return zHousingDtos;
    }


    //根据id查房源全部信息 （根据id获取房源所有信息）
    public zHousingIdDTO getzhousingIdDTO(Integer id) {

        zHousingIdDTO zHousingIdDTOs = new zHousingIdDTO();//声明对象

        zHousing zHousings = zHousingMapper.getzHousingBy(id);//根据id查房源信息

        zSite zSites = zHousingMapper.getzSites(zHousings.getzSiteId());//根据房源信息中地址外键查询地址

        zStorey zStoreys = zHousingMapper.getzStoreys(zHousings.getzStoreyId());//根据房源信息中的楼层外键查询楼层

        zMark zMarkIds = zHousingMapper.getzMarks(zHousings.getzMarkId());//根据房源信息中的楼栋号外键查询楼栋号

        zMoney zMoneys = zHousingMapper.getzMoneys(zHousings.getzMoneyId());//根据房源信息中的租金外键查询租金

        zImage zImages = zHousingMapper.getzImages(zHousings.getzImageId());//根据房源信息中的图片外键查询图片

        BeanUtils.copyProperties(zHousings, zHousingIdDTOs);//将 zHousings 中的数据拷贝到 zHousingIdDTOs 中

        zHousingIdDTOs.setzSite(zSites);//将地址放入集合中

        zHousingIdDTOs.setzStorey(zStoreys);//将楼层放入集合中

        zHousingIdDTOs.setzMarkId(zMarkIds);//将楼栋号放入集合中

        zHousingIdDTOs.setzMoney(zMoneys);//将租金放入集合中

        zHousingIdDTOs.setzImage(zImages);//将图片放入集合中

        return zHousingIdDTOs;
    }


    //公交模糊查询 （公交线路   或  公交站 ）
    public List<zHousingDTO> getzHousingDTOGJ(String ins) {
        List<zHousingDTO> zHousingDtos = new ArrayList<>();//房源信息（展示集合 循环展示）
        System.err.println("线路" + ins);
        List<zHousing> zHousings = zHousingMapper.zHousingGJ(ins);//根据公交车号，查询房源
        zHousingFF(zHousings, zHousingDtos);
        return zHousingDtos;
    }


    //关键字查询（区域   小区名 ）
    public List<zHousingDTO> getzHousingDTOGJZ(String z_area, String z_illage) {

        List<zHousingDTO> zHousingDtos = new ArrayList<>();//房源信息（展示集合 循环展示）

        if (z_area != null && z_area != "") {
            String ins = "%" + z_area + "%";//模拟前端输入的值加拼接
            List<zSite> zSiteLists = zHousingMapper.getzSitesListsa(ins);//根据区域模糊查询。
            for (zSite zSiteList : zSiteLists) {
                List<zHousing> zHousings = zHousingMapper.zSHousingSite(zSiteList.getId());//根据区域查房源
                zHousingFF(zHousings, zHousingDtos);

            }

        }

        if (z_illage != null && z_illage != "") {
            String ins = "%" + z_illage + "%";//模拟前端输入的值加拼接
            List<zHousing> zHousings = zHousingMapper.zHousingsill(ins);//根据  小区名 查房源
            zHousingFF(zHousings, zHousingDtos);
        }
        return zHousingDtos;

    }


    //区域多条件查询(只有一个条件查询)       区域            租金（两个）         户型              方式         朝向
    public List<zHousingDTO> getzHousingDTOQY(String z_area, String z_rent1, String z_rent2, String z_house, String
            z_mode, String z_towards) {

        List<zHousingDTO> zHousingDtos = new ArrayList<>();//房源信息（展示集合 循环展示）

        //-----------------------------------------------------------------------------------------------------区域
        if (z_area != null && z_area != "") {//根据区域查询房源
            String zArea = "%" + z_area + "%";//拼接模糊查询
            List<zSite> zSites = zHousingMapper.zSites(zArea);
            for (zSite z : zSites) {
                List<zHousing> zHousings = zHousingMapper.zSHousingSite(z.getId());//根据区域查房源
                zHousingFF(zHousings, zHousingDtos);
            }
        }
        //-----------------------------------------------------------------------------------------------------租金
        if (z_rent1 != "" && z_rent1 != null && z_rent2 != "" && z_rent2 != null) {
            int zRent1 = Integer.parseInt(z_rent1);
            int zRent2 = Integer.parseInt(z_rent2);
            if (zRent2 > zRent1) {//根据租金查询房源
                List<zMoney> zMoneys = zHousingMapper.zhousingMoney(zRent1, zRent2);//根据租金查询租金id
                for (com.alibaba.ai.entity.zMoney zMoney : zMoneys) {
                    List<zHousing> zHousings = zHousingMapper.zHousingMoneyS(zMoney.getId());//根据租金id获取房源
                    zHousingFF(zHousings, zHousingDtos);
                }


            }
        }
        //-----------------------------------------------------------------------------------------------------户型
        if (z_house != null && z_house != "") {
            System.err.println("进居室");
            String ins = null;
            switch (z_house) {
                case "1居":
                    z_house = "1室";
                    ins = "%" + z_house + "%";
                    break;
                case "2居":
                    z_house = "2室";
                    ins = "%" + z_house + "%";
                    break;
                case "3居":
                    z_house = "3室";
                    ins = "%" + z_house + "%";
                    break;
                case "4居":
                    z_house = "4室";
                    ins = "%" + z_house + "%";
                    break;
            }
            List<zHousing> zHousings = zHousingMapper.getzHousingHX(ins);
            zHousingFF(zHousings, zHousingDtos);


        }
        //-----------------------------------------------------------------------------------------------------方式
        if (z_mode != null && z_mode != "") {

            List<zHousing> zHousings = zHousingMapper.getzHOUsingFS(z_mode);//根据方式查房源
            zHousingFF(zHousings, zHousingDtos);

        }
        //-----------------------------------------------------------------------------------------------------朝向
        if (z_towards != null && z_towards != "") {
            List<zHousing> zHousings = zHousingMapper.getzHCX(z_towards);//根据朝向查id
            zHousingFF(zHousings, zHousingDtos);


        }


        return zHousingDtos;
    }


    //地铁查询(只有一个条件查询)  地铁线路    地铁站     距离（两个）    区域      租金（两个）       户型          方式       朝向
    public List<zHousingDTO> getzHousingDTODT(String z_subway, String z_station, String z_distance1, String z_distance2, String z_rent1, String z_rent2, String z_house, String z_mode, String z_towards) {

        List<zHousingDTO> zHousingDtos = new ArrayList<>();//房源信息（展示集合 循环展示）
//==============================================================================================================线路查询
        if (z_subway != null && z_subway != "") {
            List<zSite> zSiteLists = zHousingMapper.getzSitesList(z_subway);//根据地铁线路查id
            for (zSite zSiteList : zSiteLists) {
                System.err.println("地铁线路" + zSiteList);
                List<zHousing> zHousings = zHousingMapper.zSHousingSite(zSiteList.getId());//根据区域查房源
                zHousingFF(zHousings, zHousingDtos);
            }
        }
//=============================================================================================================地铁站查询
        if (z_station != null && z_station != "") {
            List<zSite> zSiteLists = zHousingMapper.getzSitesLists(z_station);//根据地铁站查询
            for (zSite zSiteList : zSiteLists) {
                System.err.println("地铁站" + zSiteList);
                List<zHousing> zHousings = zHousingMapper.zSHousingSite(zSiteList.getId());//根据区域查房源
                zHousingFF(zHousings, zHousingDtos);

            }

        }

//===========================================================================================================地铁距离查询
        if (z_distance1 != null && z_distance1 != "" && z_distance2 != null && z_distance2 != "") {
            int distance1 = Integer.parseInt(z_distance1);
            int distance2 = Integer.parseInt(z_distance2);
            if (distance2 > distance1) {
                List<zSite> zSiteLists = zHousingMapper.getzSitesListd(distance1, distance2);//根据距离区间查询房源
                for (zSite zSiteList : zSiteLists) {
                    List<zHousing> zHousings = zHousingMapper.zSHousingSite(zSiteList.getId());//根据区域查房源
                    zHousingFF(zHousings, zHousingDtos);
                }

            }

        }

        //-----------------------------------------------------------------------------------------------------租金
        if (z_rent1 != "" && z_rent1 != null && z_rent2 != "" && z_rent2 != null) {
            int zRent1 = Integer.parseInt(z_rent1);
            int zRent2 = Integer.parseInt(z_rent2);
            if (zRent2 > zRent1) {//根据租金查询房源
                List<zMoney> zMoneys = zHousingMapper.zhousingMoney(zRent1, zRent2);//根据租金查询租金id
                for (com.alibaba.ai.entity.zMoney zMoney : zMoneys) {
                    List<zHousing> zHousings = zHousingMapper.zHousingMoneyS(zMoney.getId());//根据租金id获取房源
                    zHousingFF(zHousings, zHousingDtos);
                }


            }
        }
        //-----------------------------------------------------------------------------------------------------户型
        if (z_house != null && z_house != "") {
            System.err.println("进居室");
            String ins = null;
            switch (z_house) {
                case "1居":
                    z_house = "1室";
                    ins = "%" + z_house + "%";
                    break;
                case "2居":
                    z_house = "2室";
                    ins = "%" + z_house + "%";
                    break;
                case "3居":
                    z_house = "3室";
                    ins = "%" + z_house + "%";
                    break;
                case "4居":
                    z_house = "4室";
                    ins = "%" + z_house + "%";
                    break;
            }
            List<zHousing> zHousings = zHousingMapper.getzHousingHX(ins);
            zHousingFF(zHousings, zHousingDtos);

        }
        //-----------------------------------------------------------------------------------------------------方式
        if (z_mode != null && z_mode != "") {

            List<zHousing> zHousings = zHousingMapper.getzHOUsingFS(z_mode);//根据方式查房源
            zHousingFF(zHousings, zHousingDtos);

        }
        //-----------------------------------------------------------------------------------------------------朝向
        if (z_towards != null && z_towards != "") {
            List<zHousing> zHousings = zHousingMapper.getzHCX(z_towards);//根据朝向查id
            zHousingFF(zHousings, zHousingDtos);

        }
        return zHousingDtos;
    }

}